<script>
export default {
  props: {
    currentPage: {
      type: Number,
      default: 1,
    },
    prePages: {
      type: Number,
      default: 0
    },
    nextPages: {
      type: Number,
      default: 0
    },
    prePage: {
      type: Boolean,
      default: false,
    },
    nextPage: {
      type: Boolean,
      default: false
    }
  },

  computed: {
    hasPrePages() {
      return this.prePages && this.prePages > 0
    },

    mostPrePage() {
      return this.currentPage - this.prePages - 1
    },

    isActivePage() {
      return this.currentPage &&
        (this.currentPage !== 1 || this.nextPages > 0)
    },

    hasNextPages() {
      return this.nextPages && this.nextPages > 0
    },

    mostNextPage() {
      return this.currentPage + this.nextPages + 1
    }
  },

  methods: {
    to(page) {
      let queryString = Object.keys(this.$route.query)
        .map(key => { `${key}=${key === 'page' ? page : this.$route.query[key]}` })
        .join('&')
      if (queryString.indexOf('page=') < 0) {
        queryString = `${queryString}${queryString.length ? '&' : ''}page=${page}`
      }
      this.$router.push(`${this.$route.path}?${queryString}`)
    },
    
    calculatePrePage(idx) {
      return this.currentPage - this.prePages + idx - 1;
    },

    calculateNexPage(idx) {
      return this.currentPage + idx;
    }
  }
}
</script>

<template>
<div class="page-bar">
  <ul class="clearfix">
    <li v-if="prePage">
      <a @click="to(mostPrePage)">&laquo;</a>
    </li>
    <li v-if="hasPrePages" v-for="idx in prePages">
      <a @click="to(calculatePrePage(idx))">{{ calculatePrePage(idx) }}</a>
    </li>
    <li v-if="isActivePage" class="active">
      <a @click="to(currentPage)">{{ currentPage }}</a>
    </li>
    <li v-if="hasNextPages" v-for="idx in nextPages">
      <!-- <router-link :to="to(calculateNexPage(idx))">{{ calculateNexPage(idx) }}</router-link> -->
      <a @click="to(calculateNexPage(idx))">{{ calculateNexPage(idx) }}</a>
    </li>
    <li v-if="nextPage">
      <a @click="to(mostNextPage)">&raquo;</a>
    </li>
  </ul>
</div>
</template>

<style>
.page-bar {
  width: 100%;
  min-height: 40px;
}

.page-bar ul{
  float: right;
}

.page-bar ul > li:first-child > a {
  margin-left: 0;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}

.page-bar ul > li:last-child > a {
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
}

.page-bar li{
  list-style: none;
  display: inline
}
.page-bar li:first-child>a {
  margin-left: 0px
}
.page-bar a{
  border: 1px solid #FBD800;
  text-decoration: none;
  position: relative;
  float: left;
  padding: 6px 12px;
  margin-left: -1px;
  line-height: 1.42857143;
  color: #FBD800;
  cursor: pointer
}
.page-bar a:hover{
  background-color: #eee;
}
.page-bar .active a{
  color: #fff;
  cursor: default;
  background-color: #FBD800;
  border-color: #FBD800;
}
.page-bar i{
  font-style:normal;
  color: #d44950;
  margin: 0px 4px;
  font-size: 12px;
}
</style>